package cn.gson.springboot.controllers;

import java.text.SimpleDateFormat;  
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.pojos.DestructionYk;
import cn.gson.springboot.model.pojos.DrugPurchaseYk;
import cn.gson.springboot.model.service.YaokurukuService;
import cn.gson.springboot.model.service.YaokurukuxqService;

@Controller
public class YaokurukuController {
	@Autowired
	private YaokurukuService rs;
	@Autowired
	private YaokurukuxqService xqs;
	
	//药库入库
	@RequestMapping("yaoku-ruku")
	public void yaokuRuku(Model model) {
		//时间
		Date date = new Date();		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String hhh=sdf.format(date);
		String hhhh = hhh.toString().replace("-", "");
		//System.out.println(hhhh);
		model.addAttribute("shijian",hhhh);
		
 	}
	
	//查询所有
	@RequestMapping("rukubg")
	@ResponseBody
	public Map<String,Object> hosLikeTableSmt(
		@RequestParam("page")int page,
		@RequestParam("rows")int rows,Integer purchase_id){			
			
		//进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		//List<DrugPurchaseYk> findall=rs.findAll();
		JSONArray findall=rs.findAll(purchase_id);
		
		//进行数据格式转换和封装
		Map<String,Object> map = new HashMap<>();
		map.put("total", pg.getTotal());//封装总记录数
		map.put("rows", findall);//封装当前页的内容
			
		return map; 
	}
	 
	//采购详情查询所有
	@RequestMapping("caigxq")
	@ResponseBody
	public JSONArray findbyxqid(Integer purchase_id) {
		 JSONArray findbyxqid = xqs.findbyidxq(purchase_id);
		 System.out.println(findbyxqid.toString());
		 return findbyxqid;
	} 
	
	//修改
	@PostMapping("shtg")
	@ResponseBody
	public String idxgzz(Integer purchaseId) {
		DrugPurchaseYk l = rs.idsh(purchaseId);
		if(l!=null) {
			l.setPurchaseStatic("已审核");
			rs.xinzzz(l);
			return "1";
		}else {
			return "0";
		}
	}
		 
}
